<style>
    .w-120 {
        width: 115px;
    }

    .conentStyle {
        padding: 10px;
        box-sizing: border-box;
    }

    .conentBg {
        padding: 20px;
        box-sizing: border-box;
        border-radius: 5px;
        background-color: #fff;
        min-height: 660px;
    }

    .borderNone {
        border: none;
    }

    .formBg {
        background-color: #F7F8FA;
        padding: 20px;
    }

    .layui-table-cell {
        height: auto;
    }

    div[lay-id="saleOrderTable"] .layui-table-cell,
    div[lay-id="saleOrderTable"] .layui-form,
    div[lay-id="saleOrderTable"] .layui-table-box,
    div[lay-id="saleOrderTable"] .layui-table-body {
        overflow: visible;
    }

    div[lay-id="deliverProductTable"] td[data-field="productPack"] .layui-table-cell,
    div[lay-id="deliverProductTable"] .layui-form,
    div[lay-id="deliverProductTable"] .layui-table-box,
    div[lay-id="deliverProductTable"] .layui-table-body {
        overflow: visible;
    }
</style>
<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 头部操作栏 -->
            <form class="layui-form">
                <div class="layui-form lay-header">
                    <div class="toolbar">
                        <div class="layui-form-item lay-query-content">
                            <div class="lay-query-box">
                                <div class="query-lt">
                                    <div class="layui-inline">
                                        <label class="layui-form-label w-auto">计划单编号：</label>
                                        <div class="layui-input-inline">
                                            <input name="code" class="layui-input" type="text"
                                                   placeholder="计划单编号"/>
                                        </div>
                                    </div>
                                    <!-- <div class="layui-inline">
                                        <label class="layui-form-label w-auto">订单类型：</label>
                                        <div class="layui-input-inline">
                                            <select name="typeCode" id="typeCode" lay-filter="typeCode" lay-search class="common-select-search">
                                                <option value="">所有类型</option>
                                                <option value="PDC">盘点出库</option>
                                                <option value="DBC">调拨出库</option>
                                                <option value="SCDC">生产订单出库</option>
                                                <option value="CPC">成品销售出库</option>
                                                <option value="SCC">生产领料出库</option>
                                                <option value="THC">采购退换货出库</option>
                                            </select>
                                        </div>
                                    </div> -->
                                    <div class="layui-inline">
                                        <label class="layui-form-label w-auto">订单编号：</label>
                                        <div class="layui-input-inline">
                                            <input name="orderCode" class="layui-input" type="text"
                                                   placeholder="订单编号"/>
                                        </div>
                                    </div>
                                    <div class="layui-inline">
                                        <label class="layui-form-label w-auto">客户名称：</label>
                                        <div class="layui-input-inline">
                                            <select name="customerId" id="customerId" lay-filter="customerId" lay-search class="common-select-search">   </select>
                        
                                        </div>
                                    </div>
                                    <div class="layui-inline">
                                        <label class="layui-form-label w-auto">物流单号：</label>
                                        <div class="layui-input-inline">
                                            <input name="expressCode" class="layui-input" type="text"
                                            placeholder="物流单号"/>
                                        </div>
                                    </div>
                                </div>
                                <div class="query-rt">
                                    <div class="layui-inline">
                                        <button type="button" id="deliverySearch" class="layui-btn icon-btn color-green"
                                                lay-filter="deliveryPlanSearch" lay-submit>查询
                                        </button>
                                        <button type="button" class="layui-btn icon-btn color-white" lay-filter="resets" lay-submit>
                                            重置
                                        </button>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="lay-btn-rows">
                        <div class="lay-btn-box">
                            <div class="lay-btn-lt">
                                <button type="button" id="deliveryRecordBtnAdd" class="layui-btn icon-btn color-reseda">
                                    添加交付发货记录
                                </button>
                            </div>
                            <div class="lay-btn-rt">
                            </div>
                        </div>
                    </div>
                </div>
            </form>
            <!-- 表格 -->
            <table class="layui-table" id="deliveryRecordTable" lay-filter="deliveryRecordTable"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="deliveryRecordTableBar">
    <a class="layui-btn layui-btn-xs" lay-event="details">详情</a>
    <!--<a class="layui-btn layui-btn-xs" lay-event="">退货</a>
    <a class="layui-btn layui-btn-xs" lay-event="">换货</a>-->
</script>
<script type="text/html" id="deliverProductTableBar">
    <a class="layui-btn layui-btn-xs" lay-event="details">详情</a>
    <!--<a class="layui-btn layui-btn-xs" lay-event="">保存</a>
    <a class="layui-btn layui-btn-xs" lay-event="">修改</a>-->
</script>

<!-- 表单弹窗 -->
<script type="text/html" id="deliveryRecordForm">
    <!-- 正文开始 -->
    <div class="layui-fluid">
        <div class="layui-card">
            <div class="layui-card-body">
                <form lay-filter="deliveryRecordForm" id="deliveryRecordForm2" class="layui-form model-form"
                      style="padding: 0px 0px 0px 0px">
                    <div class="layui-row table-bor" style="margin-top: 20px">
                        <input name="inoutTypeCode" id="inoutTypeCode" value="marketdOut" type="hidden"/>
                        <table class="layui-table" id="logisticsTable">
                            <thead>
                            <tr>
                                <th style="text-align: center;">交付计划</th>
                                <th style="text-align: center;">物流方式</th>
                                <th style="text-align: center;">物流/快递公司</th>
                                <th style="text-align: center;">物流单号</th>
                                <th style="text-align: center;">运费</th>
                                <th style="text-align: center;">物流联系人</th>
                                <th style="text-align: center;">物流电话</th>
                                <th style="text-align: center;">车牌号</th>
                            </tr>
                            </thead>
                            <tbody>
                            <tr>
                                <td style="text-align: center;">
                                    <select name="planId" id="deliveryPlanId" lay-filter="deliveryPlanId"
                                            lay-verify="required" class="setSelectDisabled"
                                            style="text-align: center;"></select>
                                </td>
                                <td style="text-align: center;">
                                    <select name="transportType" required lay-verify="required"
                                            lay-filter="transportType" placeholder="请选择运输方式"
                                            class="setSelectDisabled" style="text-align: center;">
                                        <option value="">请选择运输方式</option>
                                        <option value="logistics">物流运输</option>
                                        <option value="express">快递运输</option>
                                        <option value="self">自行运输</option>
                                    </select>
                                </td>
                                <td style="text-align: center;">
                                    <select name="companyId" id="companyIdOption"
                                            placeholder="请选择物流企业"
                                            class="setSelectDisabled"
                                            style="text-align: center;"></select>
                                </td>
                                <td style="text-align: center;">
                                    <input type="text" name="expressCode" id="expressCode"
                                           class="layui-input setInputDisabled"
                                           style="text-align: center;">
                                </td>
                                <td style="text-align: center;">
                                    <input type="text" name="transportExpenses"
                                           id="transportExpenses" placeholder="单位/元"
                                           class="layui-input setInputDisabled"
                                           style="text-align: center;">
                                </td>
                                <td style="text-align: center;">
                                    <input type="text" name="customerConcat"
                                           id="customerConcat" autocomplete="off"
                                           class="layui-input setInputDisabled"
                                           style="text-align: center;">
                                </td>
                                <td style="text-align: center;">
                                    <input type="text" name="customerMoble"
                                           id="customerMoble" autocomplete="off"
                                           class="layui-input setInputDisabled"
                                           lay-verify="phone" style="text-align: center;">
                                </td>
                                <td style="text-align: center;">
                                    <input type="text" name="carNo" id="carNo"
                                           autocomplete="off"
                                           class="layui-input setInputDisabled"
                                           style="text-align: center;">
                                </td>
                            </tr>
                            </tbody>

                        </table>
                        <table class="layui-table" id="saleOrderTable" lay-filter="saleOrderTable">
                            <thead>
                            <tr>
                                <th style="text-align: center;">订单编号</th>
                                <th style="text-align: center;">客户名称</th>
                                <th style="text-align: center;">订单状态</th>
                                <th style="text-align: center;">订单合同编号</th>
                                <th style="text-align: center;">定制物料数量</th>
                                <th style="text-align: center;">客户联系人</th>
                                <th style="text-align: center;">订单备注</th>
                                <!--<th>操作</th>-->
                            </tr>
                            </thead>
                            <tbody>
                            <tr>
                                <td><select name="saleOrderId" id="saleOrderId" lay-filter="saleOrderId"
                                            lay-verify="required" class="setSelectDisabled"></select></td>
                                <td><input type="text" name="selOrderCustomerName" id="customerName"
                                           class="layui-input borderNone" disabled></td>
                                <td><input type="text" name="orderStatusDesc" id="orderStatusDesc"
                                           class="layui-input borderNone" disabled></td>
                                <td><input type="text" name="contractCode" id="contractCode"
                                           class="layui-input borderNone" disabled></td>
                                <td><input type="text" name="proQuantity" id="proQuantity"
                                           class="layui-input borderNone" disabled></td>
                                <td><input type="text" name="customerContactsName" id="customerContactsName"
                                           class="layui-input borderNone" disabled></td>
                                <td><input type="text" name="selOrderRemark" id="selOrderRemark"
                                           class="layui-input borderNone" disabled></td>
                                <!--<td><a class="layui-btn layui-btn-xs" lay-event="saleOrderDetail">详情</a></td>-->
                            </tr>
                            </tbody>
                        </table>
                    </div>

                    <div class="layui-form-item text-left" style="margin-top: 60px;">
                        <button id="deliveryProductBtnAdd" type="button" class="layui-btn icon-btn">
                            <i class="layui-icon">&#xe654;</i>添加交付物料
                        </button>
                    </div>
                    <div class="layui-row table-bor" style="margin-top: 20px">
                        <table class="layui-table" id="deliverProductTable" lay-filter="deliverProductTable"></table>
                    </div>

                    <div class="layui-form-item text-center" style="margin-left: 134px;">
                        <button class="layui-btn" lay-filter="deliveryRecordFormSubmit" id="deliveryRecordFormSubmit"
                                lay-submit>提交
                        </button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</script>

<!--包装方式下拉框-->
<script type="text/html" id="packingTemplet">
    <select name="productPack" id="productPack" lay-filter="productPack" lay-verify="required"></select>
</script>

<!--交付商品信息-->
<script type="text/html" id="deliveryRecordProductForm">
    <div class="layui-fluid">
        <div class="layui-card">
            <div class="layui-card-body">
                <form class="layui-form" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <input name="skuSpecCode" class="layui-input" type="text" placeholder="规格码"/>
                        </div>
                        <div class="layui-inline">
                            <input name="productName" class="layui-input" type="text" placeholder="物料名称"/>
                        </div>
                        <div class="layui-inline">
                            <input name="skuCode" class="layui-input" type="text" placeholder="物料编号"/>
                        </div>
                        <div class="layui-inline">
                            <button class="layui-btn icon-btn" lay-submit=""
                                    lay-filter="productSearch"><i class="layui-icon">&#xe615;</i>搜索
                            </button>
                        </div>
                    </div>
                </form>
                <table class="layui-table" id="productTable" lay-filter="productTable" style="height: 0px;"></table>
                <div class="layui-form-item" style="margin-top: 10px;text-align: center;">
                    <div class="layui-inline">
                        <button id="productConfirmBtn" type="button" lay-event="getCheckData"
                                class="layui-btn icon-btn">确定提交
                        </button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</script>
<!-- js部分 -->
<script>
    layui.use(['layer', 'form', 'table', 'admin', 'laydate'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var admin = layui.admin;
        var upload = layui.upload;
        var laydate = layui.laydate;
        var type = 0; //0是添加 1是详情
        var deliverProductList = [];//出库物料数据
        var outData = null;
        var detailData = null;

        // 渲染表格
        var insTb = table.render({
            elem: '#deliveryRecordTable',
            url: config.base + 'enterprise/sell/admin/deliveryRecord/list',
            page: true,
            cellMinWidth: 100,
            cols: [[
                {type: 'numbers', align: 'center', title: '序号'}
                , {field: 'recordCode', align: 'center', title: '交付记录编号'}
                , {field: 'expressCode', align: 'center', title: '物流单号', width: 160}
                , {field: 'transportTypeDesc', align: 'center', title: '物流方式'}
                , {field: 'customerName', align: 'center', title: '客户名称'}
                // , {field: 'expressCode', align: 'center', title: '物流公司',}
                // , {field: 'expressCode', align: 'center', title: '物流单号',}
                , {field: 'saleOrderCode', align: 'center', title: '所属订单号', }
                , {field: 'transportExpenses', align: 'center', title: '物流费用'}
                , {field: 'shipmentQty', align: 'center', title: '发货物料数'}
                , {field: 'subtotalAmount', align: 'center', title: '发货金额'}
                // , {field: 'remark', align: 'center', title: '备注'}
                , {field: 'creatorName', align: 'center', title: '添加人'}
                , {field: 'createTime', align: 'center', title: '添加时间'}
                , {
                    field: 'status', align: 'center', title: '状态', templet: function (d) {
                        if (d.status == 0) {
                            return d.status = "待签收";
                        } else {
                            return d.status = "已签收";
                        }
                    }
                }
                , {align: 'center', toolbar: '#deliveryRecordTableBar', title: '操作', width: 100}
            ]],
            done: function (res, curr, count) {

            }
        });

        // 工具条点击事件
        table.on('tool(deliveryRecordTable)', function (obj) {
            var data = obj.data;
            if (obj.event === 'details') { //详情
                // type = 1;
                // outData = data;
                // showEditModel(type, outData);
                admin.req('enterprise/sell/admin/deliveryRecord/detail/' + data.id, {}, (res) => {
                    if (res.code === 200) {
                        openForm('detail', res.data)
                    }
                })
            } else if (obj.event === 'audit') { // 审核
                doDelete(obj);
            }
        });

        //监听排序
        table.on('sort(deliveryRecordTable)', function (obj) {
            table.reload('deliveryRecordTable', {
                initSort: obj
                , where: {
                    sort: obj.field
                    , order: obj.type
                }
            });
        });

        // 搜索
        form.on('submit(deliveryRecordSearch)', function (data) {
            insTb.reload({where: data.field, page: {curr: 1}}, 'data');
            return false;
        });

        // 添加按钮点击事件
        $('#deliveryRecordBtnAdd').click(function () {
            // type = 0;
            // outData = null;
            // showEditModel(type, outData);
            openForm('add')

        });

        function openForm(evetName, data) {
            const titleObj={
                add:'添加交付记录',
                edit:'修改交付记录',
                detail:'查看交付记录'
            }
            admin.formOpen({
                area: ['90%', '85%'],
                title:titleObj[evetName],
                offset: '50px',
                path: 'components/sell/deliveryRecordForm.html',
                success() {
                    parentData.eventName = evetName
                    parentData.data = data
                }
            })
        };
        admin.initDataSelect("enterprise/sell/admin/customersInfo/getBasicInfo", {customerType:"2,3"}, "customerId", null, "客户信息", "customerName","id");

        // 显示编辑弹窗
        function showEditModel(type, data) {
            admin.open({
                type: 1,
                area: ['1600px', '800px'],
                offset: ['100px', '260px'],
                title: data ? '交付记录详情' : '添加交付记录',  
                content: $('#deliveryRecordForm').html(),
                success: function () {

                    if (type == 1) {//详情
                        //交付商品详情
                        admin.req('enterprise/sell/admin/deliveryPlan/getDeliveryPlanDetailList', {deliveryPlanId: data.planId}, function (res) {
                            if (res.code == 200) {
                                $("#salesType").val(res.data[0].salesType);
                                form.render('select');

                                deliverProductList = res.data;
                                table.reload('deliverProductTable', {
                                    data: deliverProductList,
                                })
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, 'GET');

                        //设置下拉框禁用
                        $(".setSelectDisabled").attr("disabled", "disabled");
                        //设置input禁用
                        $(".setInputDisabled").attr("disabled", "disabled");
                        $(".setInputDisabled").attr("style", "background-color: #EEEEEE;");//设置input为灰色背景

                        //设置添加物料按钮禁用
                        $("#deliveryProductBtnAdd").attr("disabled", true);
                        //隐藏提交按钮
                        $("#deliveryRecordFormSubmit").hide();

                    }

                    deliverProductList = [];//每次打开弹框清空出库物料数据

                    //获取销售订单编号
                    /* admin.req('enterprise/sell/admin/deliveryRecord/getSaleOrderInfo', {limit:9999}, function (res) {
                         if (res.code == 200) {
                             loadSelectHtml(res.data,'请选择','saleOrderId','id','code');
                             if (data) {
                                 $("#saleOrderId").val(data.saleOrderId);
                                 form.render('select');

                                 admin.req('enterprise/sell/admin/deliveryRecord/getSaleOrderInfo', {selOrderId: data.saleOrderId}, (res) => {
                                     if (res.code=='200') {
                                         let ordData = res.data[0];
                                         $("#customerName").val(ordData.customerName);
                                         $("#orderStatusDesc").val(ordData.status);
                                         $("#contractCode").val(ordData.contractCode);
                                         $("#proQuantity").val(ordData.proQuantity);
                                         $("#customerContactsName").val(ordData.customerContactsName);
                                         $("#selOrderRemark").val(ordData.selOrderRemark);
                                         //form.val('deliveryRecordForm',res.data[0]);
                                     }
                                 }, 'get');
                             }
                         }else {
                             layer.msg('获取订单失败', {icon: 2, time: 1000});
                         }
                     }, 'GET');

                     form.on('select(saleOrderId)', function (data) {//选中订单号带出订单信息
                         admin.req('enterprise/sell/admin/deliveryRecord/getSaleOrderInfo', {selOrderId: data.value}, (res) => {
                             if (res.code=='200') {
                                 let ordData = res.data[0];
                                 $("#customerName").val(ordData.customerName);
                                 $("#orderStatusDesc").val(ordData.status);
                                 $("#contractCode").val(ordData.contractCode);
                                 $("#proQuantity").val(ordData.proQuantity);
                                 $("#customerContactsName").val(ordData.customerContactsName);
                                 $("#selOrderRemark").val(ordData.selOrderRemark);
                             }
                         }, 'get');
                     });*/

                    //获取交付计划编号
                    admin.req('enterprise/sell/admin/deliveryPlan/list', {limit: 9999}, function (res) {
                        if (res.code == 200) {
                            var html = '<option value="">请选择交付计划</option>';
                            for (var k = 0; k < res.data.length; k++) {
                                html += '<option selOrderId="' + res.data[k].saleOrderId + '" value="' + res.data[k].id + '">' + res.data[k].code + '</option>';
                            }
                            $("form select[name='planId']").html(html);
                            form.render('select');

                            if (data) {
                                $("#deliveryPlanId").val(data.planId);
                                form.render('select');

                                admin.req('enterprise/sell/admin/deliveryRecord/getSaleOrderInfo', {selOrderId: data.saleOrderId}, (res) => {
                                    if (res.code == '200') {
                                        loadSelectHtml(res.data, '请选择订单编号', 'saleOrderId', 'id', 'code');
                                        $("#saleOrderId").val(data.saleOrderId);
                                        form.render('select');
                                        let ordData = res.data[0];
                                        $("#customerName").val(ordData.customerName);
                                        $("#orderStatusDesc").val(ordData.status);
                                        $("#contractCode").val(ordData.contractCode);
                                        $("#proQuantity").val(ordData.proQuantity);
                                        $("#customerContactsName").val(ordData.customerContactsName);
                                        $("#selOrderRemark").val(ordData.selOrderRemark);
                                    } else {
                                        loadSelectHtml(null, '请选择订单编号', 'saleOrderId', 'id', 'code');
                                    }
                                }, 'get');
                            }

                        } else {
                            layer.msg('获取交付计划编号失败', {icon: 2, time: 1000});
                        }

                    }, 'GET');
                    //选中计划编号号带出订单信息
                    form.on('select(deliveryPlanId)', function (data) {
                        //获取下拉框选中的销售订单编号属性
                        var saleOrderId = $("#deliveryPlanId").find("option:selected").attr("selOrderId");

                        admin.req('enterprise/sell/admin/deliveryRecord/getSaleOrderInfo', {selOrderId: saleOrderId}, (res) => {
                            if (res.code == '200') {
                                loadSelectHtml(res.data, '请选择订单编号', 'saleOrderId', 'id', 'code');
                                $("#saleOrderId").val(saleOrderId);
                                form.render('select');
                                let ordData = res.data[0];
                                $("#customerName").val(ordData.customerName);
                                $("#orderStatusDesc").val(ordData.status);
                                $("#contractCode").val(ordData.contractCode);
                                $("#proQuantity").val(ordData.proQuantity);
                                $("#customerContactsName").val(ordData.customerContactsName);
                                $("#selOrderRemark").val(ordData.selOrderRemark);
                            }
                        }, 'get');
                    });

                    //查询物流公司
                    admin.req('enterprise/invoicing/admin/transportCompany/getAll', {}, function (res) {
                        if (res.code == 200) {
                            loadSelectHtml(res.data, '请选择物流公司', 'companyId', 'id', 'name');
                            if (data) {
                                $("#companyIdOption").val(data.companyId);
                                form.render('select');
                            }
                        } else {
                            layer.msg(res.msg, {icon: 2});
                        }
                    }, 'GET');

                    //添加交付商品点击事件
                    $("#deliveryProductBtnAdd").click(function () {
                        // showProductModel();
                    })

                    let shipmentQty;//发货数量
                    let surplus;//交付剩余
                    let deliverySituation;//交付情况
                    let overstep = false;//超出
                    var deliverProductTable = table.render({
                        elem: '#deliverProductTable',
                        data: deliverProductList,
                        cellMinWidth: 100,
                        cols: [[
                            {field: 'number', title: '序号', type: 'numbers'}
                            , {field: 'id', title: 'ID', hide: true}
                            , {field: 'skuCode', title: '物料编号'}
                            // , {field: 'skuSpecValueNames', title: 'SKU规格',hide : type == 1 ? true : false}
                            // , {field: 'skuSpecCode', title: 'SKU规格',hide : type == 0 ? true : false}
                            , {field: 'skuSpecCode', title: '规格码'}
                            , {field: 'enterpriceCode', title: '客户物料编码'}
                            , {field: 'quantity', title: '计划交付'}
                            , {
                                field: 'surplus', title: '交付剩余',
                                //templet: d => '<span class="surplus" id="surplus"></span>'
                                templet: d => '<input type="text" id="surplus" value="' + d.surplus + '" class="layui-input surplus borderNone" disabled/>'
                            }
                            , {field: 'unitName', title: '计价单位'}
                            , {field: 'packName', title: '包装方案'}
                            , {field: 'baseRate', title: '包装换算', hide: true}
                            , {field: 'deliverySituation', title: '交付情况', hide: true}
                            , {
                                field: 'packNum', title: '包装数量',
                                templet: d => '<input type="text" lay-verify="required" id="packNum" value="' + d.packNum + '" class="layui-input packNum setDisabled" />'
                            }
                            // , {field: 'productPack', title: '包装方式',templet: "#packingTemplet",width: 120}
                            // , {field: 'packNum', title: '包装数量',
                            //     templet: d => '<span class="packNum" id="packNum"></span>'
                            // }
                            , {
                                field: '', title: '发货数量',
                                //templet: d => '<span class="shipmentQty" id="shipmentQty"></span>'
                                templet: d => '<input type="text" id="shipmentQty" value="' + d.shipmentQty + '" class="layui-input shipmentQty borderNone" disabled/>'
                            }
                            , {field: 'marketPrice', title: '单价'}
                            , {
                                field: 'subtotalAmount', title: '总价',
                                templet: d => '<input type="text" id="subtotalAmount" value="' + d.subtotalAmount + '" class="layui-input subtotalAmount borderNone" disabled/>'
                            }
                            , {
                                field: 'remark', title: '备注', width: 150,
                                templet: d => '<input type="text" id="remark" value="' + d.remark + '" class="layui-input remark setDisabled" />'
                            }
                            /*,{align: 'center', toolbar: '#deliverProductTableBar', title: '操作', width: 200}*/
                        ]]
                        , done: function (res, curr, count) {

                            $("body").delegate(".packNum", "change", function () {
                                let quantity = $(this).parents('tr').find('td[data-field="quantity"]').text();
                                let baseRate = $(this).parents('tr').find('td[data-field="baseRate"]').text();
                                deliverySituation = $(this).parents('tr').find('td[data-field="deliverySituation"]').text();
                                let packNum = $(this).val();

                                shipmentQty = packNum * baseRate;//发货数量
                                if (deliverySituation == 0) {//未交付，剩余量=计划数量-发货量

                                    surplus = quantity - shipmentQty;//交付剩余
                                    if (surplus < 0) {//超出计划数量
                                        overstep = true;
                                        //layer.msg("超出计划数量，请重新输入",{icon: 2});
                                    } else {
                                        overstep = false;
                                        $(this).parents('tr').find('.surplus').val(surplus);
                                    }
                                }

                                if (deliverySituation == 1) {//交付有剩余，剩余量=目前剩余量-发货量

                                    let oldSurplus = $(this).parents('tr').find('.surplus').val();
                                    if (shipmentQty <= oldSurplus) {
                                        overstep = false;
                                        surplus = oldSurplus - shipmentQty;//交付剩余
                                        $(this).parents('tr').find('.surplus').val(surplus);
                                    } else {
                                        overstep = true;
                                        //layer.msg("超出剩余数量，请重新输入",{icon: 2});
                                    }
                                }

                                $(this).parents('tr').find('.shipmentQty').val(shipmentQty);

                                //计算发货总价
                                let mPrice = $(this).parents('tr').find('td[data-field="marketPrice"]').text();
                                $(this).parents('tr').find('.subtotalAmount').val(mPrice * shipmentQty);

                                deliverProductList[$(this).parents('tr').index()].packNum = $(this).val();
                                deliverProductList[$(this).parents('tr').index()].subtotalAmount = $(this).parents('tr').find('.subtotalAmount').val();
                                deliverProductList[$(this).parents('tr').index()].shipmentQty = $(this).parents('tr').find('.shipmentQty').val();
                                deliverProductList[$(this).parents('tr').index()].surplus = $(this).parents('tr').find('.surplus').val();
                            });


                            $("body").delegate(".remark", "change", function () {
                                deliverProductList[$(this).parents('tr').index()].remark = $(this).val();
                            });

                            if (data) {
                                //设置禁用
                                $(".setDisabled").attr("disabled", "disabled");
                                $(".setDisabled").attr("style", "background-color: #EEEEEE;");//设置input为灰色背景
                            }

                        }
                    });


                    form.val('deliveryRecordForm', data);

                    // 表单提交事件
                    form.on('submit(deliveryRecordFormSubmit)', function (d) {
                        if (type == 0) {//添加

                            if (deliverProductList.length == 0) {
                                layer.msg("请先添加出库商品", {icon: 2});
                                return false;
                            }
                            //第一层判断：数量是否超出
                            for (var i = 0; i < deliverProductList.length; i++) {
                                var quantity = deliverProductList[i].quantity;
                                var shipmentQty = deliverProductList[i].shipmentQty;
                                var surplus = deliverProductList[i].surplus;

                                if (deliverySituation == 0) {
                                    if (shipmentQty > quantity) {
                                        layer.msg("超出计划交付量，请重新输入", {icon: 2});
                                        return false;
                                    }
                                }
                                if (deliverySituation == 1) {
                                    if (shipmentQty > surplus) {
                                        layer.msg("超出剩余数量，请重新输入", {icon: 2});
                                        return false;
                                    }
                                }
                            }

                            //第二层判断：改变了包装数值，判断数量是否超出
                            if (deliverySituation == 0 && overstep) {
                                layer.msg("超出计划交付量，请重新输入", {icon: 2});
                                return false;
                            }

                            if (deliverySituation == 1 && overstep) {
                                layer.msg("超出剩余数量，请重新输入", {icon: 2});
                                return false;
                            }

                            d.field.productJson = JSON.stringify(deliverProductList);
                            console.log("物料数据", deliverProductList)

                            layer.load(2);
                            //enterprise/sell/admin/deliveryRecord/add
                            admin.req('enterprise/sell/admin/deliveryRecord/add', d.field, function (res) {
                                layer.closeAll('loading');
                                if (res.code == 200) {
                                    layer.msg(res.msg, {icon: 1});
                                    table.reload('deliveryRecordTable');
                                    layer.closeAll('page');
                                } else {
                                    layer.msg(res.msg, {icon: 2});
                                }
                            }, 'POST');
                        }
                        return false;
                    });
                }
            });
        }
        form.on("submit(deliveryPlanSearch)", function (data) {
            table.reload("deliveryRecordTable", {
                where: data.field,
                page: { curr: 1 }
            });
        });
        form.on("submit(resets)", function (data) {
            $(".lay-header").find("select").val("");
            $(".lay-header").find("input").val("");
            let field = data.field;
            for (let key in field) {
                field[key] = "";
            }
            table.reload("deliveryRecordTable", {
                where: field,
                page: { curr: 1 }
            });
        });
        //enterprisecab/order/admin/inventoryOutPlanDetail/list
        function showProductModel() {
            var index = admin.open({
                type: 1,
                area: ['1400px', '830px'],
                offset: ['80px', '350px'],
                title: '添加交付计划商品信息',
                content: $('#deliveryRecordProductForm').html(),
                success: function () {
                    form.render('select');
                    var productTable = table.render({
                        elem: '#productTable',
                        url: config.base + 'enterprise/sell/admin/deliveryPlan/getDeliveryPlanDetailList',
                        page: true,
                        cellMinWidth: 100,
                        cols: [[
                            {type: 'checkbox'}
                            , {field: 'number', title: '序号', width: 50, type: 'numbers'}
                            , {
                                field: 'skuPic',
                                align: 'center',
                                title: '图片',
                                width: 100,
                                templet: '<div><img src="{{ d.skuPic}}" style="height: 80px;width: 80px;"></div>'
                            }
                            , {field: 'productName', align: 'center', title: '物料名称', width: 120}
                            , {field: 'skuCode', align: 'center', title: '物料编号', width: 150}
                            , {field: 'skuSpecCode', align: 'center', title: '规格码'}
                            , {field: 'productTypeName', align: 'center', title: '物料类型'}
                            , {field: 'marketPrice', align: 'center', title: '物料单价'}
                            , {field: 'unitName', align: 'center', title: '计价单位'}
                            , {field: 'quantity', align: 'center', title: '计划交付数量', width: 130}
                            , {field: 'packNum', align: 'center', title: '包装数量'}
                            , {field: 'quantity', align: 'center', title: '发货数量'}
                            , {field: 'remark', align: 'center', title: '备注'}
                        ]]
                    });
                    //查询物料类型
                    admin.req('enterprise/product/admin/productType/getProductTypeVOAll', {}, function (res) {
                        if (res.code == 200) {
                            if (res.data.length > 0) {
                                $.each(res.data, function (index, item) {
                                    $("#productTypeId").append(new Option(item.name, item.id));
                                })
                                form.render('select');
                            }
                        } else {
                            layer.msg(res.msg, {icon: 2});
                        }
                    }, 'GET');
                    //查询物料分类
                    admin.req('enterprise/product/admin/productCategory/getAll', {}, function (res) {
                        if (res.code == 200) {
                            if (res.data.length > 0) {
                                $.each(res.data, function (index, item) {
                                    $("#productCategoryId").append(new Option(item.name, item.id));
                                })
                                form.render('select');
                            }
                        } else {
                            layer.msg(res.msg, {icon: 2});
                        }
                    }, 'GET');

                    // 搜索
                    form.on('submit(productSearch)', function (data) {
                        productTable.reload({where: data.field, page: {curr: 1}}, 'data');
                        return false;
                    });
                    $("#productConfirmBtn").click(function () {
                        //头工具栏事件
                        var checkRows = table.checkStatus('productTable');
                        if (checkRows.data.length == 0) {
                            layer.msg('请选择要确认出货的商品', {icon: 2});
                            return;
                        }
                        let flag = false;
                        // 确认出货的商品
                        checkRows.data.forEach((item, index) => {
                            const row = deliverProductList.find(x => x.skuCode === item.skuCode);
                            //checkRows.data[index].quantity = 1;
                            if (row) {
                                flag = true;
                                layer.msg('选择发货的商品中有重复存在的，请检查', {icon: 2});
                            }
                        })

                        if (!flag) {
                            deliverProductList.push(...checkRows.data)
                            //确认提交物料信息，重载deliverProductList
                            table.reload('deliverProductTable', {
                                data: deliverProductList,
                            })
                            console.log("确认提交物料信息:", deliverProductList)
                            layer.close(index);
                        }

                    })

                }
            });
        }

        // 删除
        function doDelete(obj) {
            layer.confirm('确定要审核吗？', {
                offset: '65px',
                skin: 'layui-layer-admin'
            }, function (i) {
                layer.close(i);
                layer.load(2);
                admin.req('enterprisecab/order/admin/inventoryOutPlan/updateStatus', {id: obj.data.id}, function (res) {
                    layer.closeAll('loading');
                    if (res.code == 200) {
                        layer.msg(res.msg, {icon: 1});
                        table.reload('deliveryRecordTable', {})
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'PUT');
            });
        }

        // 导出excel
        $('#inventoryOutPlanBtnExport').click(function () {
            var checkRows = table.checkStatus('deliveryRecordTable');
            if (checkRows.data.length == 0) {
                layer.msg('请选择要导出的数据', {icon: 2});
            } else {
                table.exportFile(insTb.config.id, checkRows.data, 'xls');
            }
        });

        /**
         * 加载地址HTML
         * @param data 下拉菜单数据
         * @param tips select 默认提示
         * @param htmltarget html要插入的位置
         * @param name  下拉框需要显示值的属性
         * @param value 下拉框value取值
         */
        function loadSelectHtml(data, tips, htmltarget, value, name) {
            let html = ' <option value>' + tips + '</option>';
            if (!data || data == null) {
                return;
            }
            for (let i = 0; i < data.length; i++) {
                html += `<option value='${data[i][value]}'>${data[i][name]}</option>`;
            }
            $("[name='" + htmltarget + "']").html(html);
            form.render('select');
        }

    });
</script>